package com.study.manage.dal;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.study.manage.domain.News;
import com.study.manage.domain.enumm.EvaluateNewType;
import com.study.manage.domain.enumm.LiveDieState;

public interface NewsRepository extends PagingAndSortingRepository<News, Long> {

    @Query("select t from News t where t.type in (:types) and t.userId=:userId")
    Page<News> findByTypeInAndUserId(@Param("types") List<EvaluateNewType> types,
            @Param("userId") Long userId, Pageable pageable);

    List<News> findByBlogId(Long blogId);

    @Query("select t from News t,EvaluateNew en where en.newsId=t.id and t.state=0 and en.state=0 and t.type in (:types) and t.userId=:userId and en.evaluateId=:evaluateId")
    Page<News> findByTypeInAndUserIdAndEvaluateId(@Param("types") List<EvaluateNewType> types,
            @Param("userId") Long userId, @Param("evaluateId") Long evaluateId, Pageable pageable);

    @Query("select t from News t where (t.title like %:title% or :title is null) and (to_days(t.createdAt)=to_days(:date) or :date is null)"
            + " and (t.state=:state or :state is null)  and (t.userId=:userId or :userId is null) and (t.type=:type or :type is null)")
    Page<News> findByTitleAndDateAndUserIdAndType(@Param("title") String title,
            @Param("date") Date date, @Param("state") LiveDieState state,
            @Param("userId") Long userId, @Param("type") EvaluateNewType type, Pageable pageable);
}
